package com.longrise.android.byjk.plugins.login.verifyphnumber;


import android.text.TextUtils;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.utils.PhoneUtils;
import com.longrise.common.datasource.remote.LoadDataManager;

/**
 * Created by callxkj on 2017/2/13.
 * 逻辑处理
 */

public class VerifyPhnumberPresenter extends VerifyPhnumberContract.Presenter {

    @Override
    public void init() {

    }


    @Override
    public void toConfirm(String mobile) {
       if(TextUtils.isEmpty(mobile)){
           mView.onOnFail("手机号码不能为空");
           return;
       }
        if (!PhoneUtils.isPhoneNumber(mobile)) { // 判断是否是电话号码
            mView.onOnFail("手机号码格式错误");
            return;
        }
        UserInfor userInfor = UserInfor.getInstance();
        EntityBean bean = new EntityBean();
        bean.set("userid", userInfor.getUserid());
        bean.set("precardno", userInfor.getUsersfzh());
        bean.set("mobile", mobile);
        final EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService("", UrlConstants.BaseUrl,
                "bbt_user_uPersonInfo", fbean, new LoadDataManager.OnRequestCallListener() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    int resultstate = (Integer) bean.get("resultstate");
                    String resultdesc = (String) bean.get("resultdesc");
                    if (resultstate == 1) {
                        mView.onSuccess();
                    } else {
                        mView.onOnFail(resultdesc);
                    }
                } catch (Exception e) {
                    mView.onOnFail("解析返回数据失败");
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished(String key, String service) {
                   mView.onComplete();
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                mView.onOnFail(mContext.getResources().getString(R.string.networkerror));
            }
        });



    }
}
